package com.kelvin.paopao.module.login.register.registerStep2;

import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.utils.RxHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/8.
 */

public class RegisterStep2Presenter implements IRegisterStep2Presenter {
    private RegisterStep2Activity mView;

    public RegisterStep2Presenter(RegisterStep2Activity view) {
        this.mView = view;
    }


    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }

    @Override
    public void getValidation(String phone) {
        RetrofitService.getValidation(phone, "zhuce")
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        mView.enableRetry(false);
                    }
                }).subscribeOn(AndroidSchedulers.mainThread()) // 指定主线程
                .subscribe(new Consumer<CommonListResponse>() {
                    @Override
                    public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                        if (commonListResponse.getReason().equals("成功")) {
                            RxHelper.countdown(60).subscribe(new Consumer<Integer>() {
                                @Override
                                public void accept(@NonNull Integer integer) throws Exception {
                                    mView.updateTextView(integer);
                                }
                            });
                        } else {
                            mView.enableRetry(true);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        ToastUtil.showToast(mView, "发送验证码失败");
                        mView.enableRetry(true);
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {

                    }
                });
    }
}
